﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ASPItalia.ModelVirtualCasting.WCF.Contracts;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.ServiceModel.Syndication;

namespace ASPItalia.ModelVirtualCasting.WCF.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class SyndicationService : ISyndicationService
    {
        [WebGet(UriTemplate = "News/{type}")]
        public SyndicationFeedFormatter News(string type)
        {
            // Creo le informazioni principali del feed
            SyndicationFeed feed = new SyndicationFeed("Model Virtual Casting News", "Ultime notizie", new Uri("http://www.mvc.it"));
            feed.Description = new TextSyndicationContent("Ultime notizie dal mondo della moda");
            feed.Authors.Add(new SyndicationPerson("info@mvc.it"));

            // Popolo gli item
            List<SyndicationItem> items = new List<SyndicationItem>();

            feed.Items = (from e in ApplicationContext.Current.News.GetAll().ToArray()
                          orderby e.NewsDate descending
                          select new SyndicationItem(e.NewsTitle,
                              e.NewsText,
                              new Uri(String.Format("http://wwww.mvc.it/News/{0}", e.NewsID))));

            // Ritorno il formatter RSS 2.0 creato
            // sulla base del feed
            if (type == "RSS20")
                return new Rss20FeedFormatter(feed);
            else
                return new Atom10FeedFormatter(feed);
        }
    }
}
